package by.dezhits.liltweets.bo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Tweet {

	private String id;
	private String text;
	private String userName;
	private String profileImageUrl;
	private String screenName;
	private String createdAt;
	private String retweeter;
	private String imageUrl;
	private String userId;
	private String link;
	private String hashtags;

	public Tweet(String s) throws JSONException {

		boolean retweeted;
		JSONObject pTask = new JSONObject(s);
		try {
			JSONObject retweetedStatus = pTask
					.getJSONObject("retweeted_status");
			retweeter = pTask.getJSONObject("user").getString("name");
			retweeted = true;
			id = pTask.getString("id");
			userId = pTask.getJSONObject("user").getString("id");
			createdAt = pTask.getString("created_at");
			pTask = retweetedStatus;
		} catch (JSONException e) {
			retweeted = false;
			retweeter = null;
		}
		if (!retweeted) {
			id = pTask.getString("id");
			userId = pTask.getJSONObject("user").getString("id");
			createdAt = pTask.getString("created_at");
		}
		text = pTask.getString("text");
		userName = pTask.getJSONObject("user").getString("name");
		profileImageUrl = pTask.getJSONObject("user").getString(
				"profile_image_url");
		screenName = pTask.getJSONObject("user").getString("screen_name");
		try {
			JSONArray urls = pTask.getJSONObject("entities").getJSONArray(
					"urls");
			if (urls.length() > 0) {
				link = urls.getJSONObject(0).getString("url");
			} else {
				throw new JSONException("No url");
			}
		} catch (JSONException e) {
			link = null;
		}

		try {
			JSONArray media = pTask.getJSONObject("entities").getJSONArray(
					"media");
			if (media.length() > 0) {
				imageUrl = media.getJSONObject(0).getString("media_url");
			} else {
				throw new JSONException("No image");
			}
		} catch (JSONException e) {
			imageUrl = null;
		}
		
		try {
			JSONArray tags = pTask.getJSONObject("entities").getJSONArray(
					"hashtags");
			if (tags.length() > 0) {
				StringBuilder sb = new StringBuilder();
				for(int i =0; i<tags.length(); i++){
					sb.append(tags.getJSONObject(i).getString("text") + "#");
				}
				int l= sb.length();
				hashtags = sb.toString().substring(0, l - 1);
			} else {
				throw new JSONException("No hashtags");
			}
		} catch (JSONException e) {
			hashtags = null;
		}

	}

	public String getId() {
		return id;
	}

	public String getText() {
		return text;
	}

	public String getUserName() {
		return userName;
	}

	public String getProfileImageUrl() {
		return profileImageUrl;
	}

	public String getScreenName() {
		return screenName;
	}

	public String getCreatedAt() {
		return createdAt;
	}

	public String getRetweeter() {
		return retweeter;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public String getUserId() {
		return userId;
	}

	public String getLink() {
		return link;
	}

	public String getHashtags() {
		return hashtags;
	}

}
